package com.vm.osm.business.service.eam.impl;

import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dto.eam.equ_part.*;
import com.vm.osm.business.service.eam.IEamEquPartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

/**
 * 设备配件信息服务实现类
 *
 * @author Xuerong Xue
 */
@Service
public class EamEquPartServiceImpl implements IEamEquPartService {
    private static final Logger LOG = LoggerFactory.getLogger(EamEquPartServiceImpl.class);
    @Value("${eam_api_url}")
    private String EAM_API_URL;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public EamEquPart get(GetEamEquPartReq request) {
        try {
            LOG.info("【设备配件】（获取配件信息）请求参数={}", request);
            if (null == request) {
                LOG.warn("【设备配件】（获取配件信息）请求参数为空");
                return null;
            }
            String URL = EAM_API_URL + "/eam/v1.0/equ_parts/" + request.getId();
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<Object> entity = new HttpEntity<>(null, headers);
            ResponseEntity<EamEquPart> exchange =
                    restTemplate.exchange(URL, HttpMethod.GET, entity, EamEquPart.class);
            return exchange.getBody();
        } catch (HttpClientErrorException e) {
            LOG.error("【设备配件】（获取配件信息）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备配件】（获取配件信息）异常", e.getMessage());
            return null;
        }
    }

    @Override
    public SearchEamEquPartRsp search(SearchEamEquPartReq request) {
        try {
            LOG.info("【设备配件】（查询配件信息）请求参数={}", request);
            if (null == request) throw new BusinessException("请求参数为空");
            String URL = EAM_API_URL + "/eam/v1.0/equ_parts/search";
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<SearchEamEquPartReq> entity = new HttpEntity<>(request, headers);
            ResponseEntity<SearchEamEquPartRsp> exchange =
                    restTemplate.exchange(URL, HttpMethod.POST, entity, SearchEamEquPartRsp.class);
            return exchange.getBody();
        } catch (HttpClientErrorException e) {
            LOG.error("【设备配件】（查询配件信息）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备配件】（查询配件信息）异常", e.getMessage());
            return null;
        }
    }

    @Override
    public List<String> batchAdd(BatchAddEamEquPartReq request) {
        try {
            LOG.info("【设备配件】（批量新增）请求参数={}", request);
            if (null == request) throw new BusinessException("请求参数为空");
            String URL = EAM_API_URL + "/eam/v1.0/equ_parts/" + request.getEnterprise_id() + "/batch_add";
            HttpHeaders headers = new HttpHeaders();
            headers.add("session_id", request.getSession_id());
            HttpEntity<BatchAddEamEquPartReq> entity = new HttpEntity<>(request, headers);
            ResponseEntity<String[]> exchange =
                    restTemplate.exchange(URL, HttpMethod.POST, entity, String[].class);
            return Arrays.asList(exchange.getBody());
        } catch (HttpClientErrorException e) {
            LOG.error("【设备配件】（批量新增）异常", e.getResponseBodyAsString());
            return null;
        } catch (Exception e) {
            LOG.error("【设备配件】（批量新增）异常", e.getMessage());
            return null;
        }
    }
}
